package com.koron.car.web.authorized.service.impl;

import com.koron.car.web.authorized.bean.entity.Revenue;
import com.koron.car.web.authorized.mapper.RevenueMapper;
import com.koron.car.web.authorized.service.RevenueService;
import com.koron.common.core.util.CodeTools;
import org.apache.commons.collections4.CollectionUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (Revenue)表服务实现类
 *
 * @author twcao
 * @since 2022-03-31 10:08:20
 */
@Service
public class RevenueServiceImpl implements RevenueService {

    /**
     * 通过ID查询单条数据
     *
     * @param operateControlId 主键
     * @return 实例对象
     */
    @Override
    public Revenue queryById(SessionFactory factory, String operateControlId) {
        RevenueMapper mapper = factory.getMapper(RevenueMapper.class);
        return mapper.queryById(operateControlId);
    }

    /**
     * 新增数据
     * @param revenue 实例对象
     * @return 实例对象
     */
    @Override
    public Revenue insert(SessionFactory factory, Revenue revenue) {
        RevenueMapper mapper = factory.getMapper(RevenueMapper.class);
        revenue.setId(CodeTools.getCode32());
        mapper.insert(revenue);
        return revenue;
    }

    @Override
    public int insertBatch(SessionFactory factory, List<Revenue> revenues) {
        if(CollectionUtils.isEmpty(revenues)) {
            return 0;
        }
        RevenueMapper mapper = factory.getMapper(RevenueMapper.class);
        revenues.stream().forEach(row -> row.setId(CodeTools.getCode32()));
        mapper.insertBatch(revenues);
        return revenues.size();
    }

    /**
     * 修改数据
     * @param revenue 实例对象
     * @return 实例对象
     */
    @Override
    public Revenue update(SessionFactory factory, Revenue revenue) {
        RevenueMapper mapper = factory.getMapper(RevenueMapper.class);
        mapper.update(revenue);
        return this.queryById(factory, revenue.getOperateControlId());
    }

    /**
     * 通过主键删除数据
     * @param operateControlId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(SessionFactory factory, String operateControlId) {
        RevenueMapper mapper = factory.getMapper(RevenueMapper.class);
        return mapper.deleteById(operateControlId) > 0;
    }
}
